<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of FolderController
 *
 * @author QuangKhoi
 */
class FolderController {
    //put your code here
    
    /**
     * Store model object
     * @var Folder
     */
    private $model;
    
    /**
     * Store view for the folder
     * @var FolderView
     */
    private $view;
    
    /**
     * Default constructor
     */
    public function __construct($model, $view) {
        $this->model = $model;
        $this->view = $view;
    }
    
    /**
     * Update folder infos or add new folder to library
     */
    public function save() {
        $model = $this->model;
        $query = "SELECT * FROM folders WHERE folderid = $model->id";
        $result = Database::getInstance()->query($query);
        if ($result->returnCode == 200)  //OK
            if ($result->dataTable->numOfRow > 0) {
                Database::getInstance()->query("UPDATE folders
                        SET foldername='$model->folderName', 
                        userid='$model->userId', 
                        foldertype='$model->folderType', 
                        parentid='$model->parentId' 
                        WHERE folderid = $model->folderId");
                return true;
            }
            else {
                Database::getInstance()->query("INSERT INTO folders
                            (userid, foldername, foldertype, parentid) VALUES
                            ('$model->userId', '$model->folderName', 
                            '$model->folderType', '$model->parentId')");
                return true;
            }
    }
    
    /**
     * Delete this folder
     */
    public function delete() {
        $model = $this->model;
        $query = "DELETE FROM folders WHERE folderid = $model->folderid";
        Database::getInstance()->query($query);
        return true;
    }
    
    /**
     * 
     * @param Int $folderId
     */
    public static function loadStructure($folderId, $folderType) {
        $db = Database::getInstance();
        if ($folderId > 0) {
            $folder = Folder::withId($folderId);
            $result = $db->query("SELECT * FROM folders WHERE parentid = '$folder->folderId' AND foldertype = '$folderType'");
            if ($result->returnCode == 200) { //OK
                if ($result->dataTable->numOfRow > 0) {
                    $folder->child = array();
                    foreach($result->dataTable->rows as $row)
                        $folder->child[] = self::loadStructure($row["folderid"], $folderType);
                }
            }
        }
        else {
            $folder = array();
            $result = $db->query("SELECT * FROM folders WHERE parentid = '0' AND foldertype = '$folderType'");
            if ($result->returnCode == 200) { //OK
                if ($result->dataTable->numOfRow > 0) {
                    $rows = $result->dataTable->rows;
                    foreach($rows as $row)
                        $folder[] = self::loadStructure($row["folderid"], $folderType);
                }
            }
        }
        return $folder;
    }
    
    public function render() {
        $folderTypes = array();
        $folders = array();
        $db = Database::getInstance();
        $result = $db->query("SELECT * FROM foldertypes");
        if ($result->returnCode == 200) {
            if ($result->dataTable->numOfRow > 0) {
                $rows = $result->dataTable->rows;
                foreach($rows as $row) {
                    $folderTypes[] = new FolderType($row["id"]);
                    $folders[] = self::loadStructure(0, $row["id"]);
                }
            }
        }
        
        $this->view->folderTypes = $folderTypes;
        $this->view->folders = $folders;
        $masterView = MasterView::getInstance();
        $masterView->folderView = $this->view;
    }
}

?>
